<?php
  session_start();
  
  require('connect.php');
  
  $frm = $_POST;
  
  //check if username exists
  if(isset($frm['send']))
  {
    if($frm['send'] == 'Send'){
      $username_input = $frm['username'];
      $password = md5($frm['password']);
      
      $log_info_fetch_query = mysql_query('SELECT gebruikers_naam, wachtwoord FROM USERNAME WHERE gebruikers_naam = "' . $username_input . '"') or die(mysql_error());
    
      //$log_row = mysql_fetch_array($log_info_fetch_query); 
      /*foreach($log_row as $key) {
         echo $key;
      } */
      while($log_row = mysql_fetch_array($log_info_fetch_query))
      {
         if($username_input == $log_row['gebruikers_naam'])
          {
            $username = $log_row['gebruikers_naam'];
          }
          if($password == $log_row['wachtwoord'])
          {
             if(isset($_SESSION['logged']))
             {
               $_SESSION['logged'] = true;
             }
             else
             {
               $_SESSION['logged'] = true;
             }
             if(isset($_SESSION['username']))
             {
                $_SESSION['username'];
             }
             else
             {
                $_SESSION['username'] = $username;
             }
             if(isset($_SESSION['password']))
             {
                $_SESSION['password'];
             }
             else
             {
                $_SESSION['password'] = $password;
             }
          }
      }
    }
  }
  if(isset($frm['logout']))
  {
    if($frm['logout'] == 'Logout')
    {
      session_destroy();
    }
  }
?>
<html>

  <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <meta name="Author" content="Wouter de Wild">
    <title>Test for login</title>    
  </head>
  <body>
    <form name="submit" method="post" action="login.php">
      <input type="text" value="Username" name="username"  onFocus="if (this.value == this.defaultValue) { this.value = ''; }"><br>
      <input type="password" name="password"><br>
      <input type="submit" name="send" value="Send">
      
      <?php
        if(isset($_SESSION['logged']))
        {
          echo('<input type="submit" name="logout" value="Logout">');
        }
      ?>
    </form>
    <?php
      if(isset($_SESSION['logged']))
      {
        echo('You are logged in as: ' . $_SESSION['username']) . '<br>';
        echo('Your password is encrypted as: ' . md5($_SESSION['password']) . '<br>');
        echo('Your password is double encrypted as: ' . md5(md5($_SESSION['password'])) . '<br>');
      }
      if(!isset($_SESSION['logged']))
      {
        echo('You are not logged in<br>');
      }
      
      print_r($_SESSION);
    ?>
   </body>             
</html>                                             